// Versi�n 2.0 del robot CoachColab

package com.wavecoler.coach.bots;

import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.wave.api.AbstractRobot;
import com.google.wave.api.Blip;
import com.google.wave.api.Gadget;
import com.google.wave.api.Wavelet;
import com.google.wave.api.event.BlipSubmittedEvent;
import com.google.wave.api.event.OperationErrorEvent;
import com.google.wave.api.event.WaveletParticipantsChangedEvent;
import com.google.wave.api.event.WaveletSelfAddedEvent;
import com.google.wave.api.event.GadgetStateChangedEvent;

public class CoachColabRobotAlan_Sprint2 extends AbstractRobot {
	
private static String usuarios = "";
private static int contador = 1;
private static String PhaseSession = "";
private static int numParticipantes = 3;
	
@Override
 protected String getRobotName() {
 return "Google Wave Robot CoachColab";
 }

@Override
 protected String getRobotAvatarUrl() {
 return "http://dl.dropbox.com/u/7268008/kickabot.jpg";
 }

 @Override
 protected String getRobotProfilePageUrl() {
 return "http://robotcoachcolab.appspot.com";
 }

 /**
 @Override
 public void onBlipSubmitted(BlipSubmittedEvent event) {
 Blip blip = event.getBlip();
 blip.append("\n You typed: "+blip.getContent());
 }
 **/

 @Override
 public void onWaveletParticipantsChanged(WaveletParticipantsChangedEvent event) {
 event.getWavelet().reply("\nSaludos, soy el Robot CoachColab.");
 }

 @Override
 public void onWaveletSelfAdded(WaveletSelfAddedEvent event) {
 Wavelet wave = event.getWavelet();
		String gadgetUrl = "http://code.google.com/p/wavecoler/source/browse/trunk/WaveColer/war/gadgets/agregausuario.xml";
		Gadget gadget = new Gadget(gadgetUrl);
		wave.getRootBlip().append(gadget);
 event.getWavelet().reply("\nHola, agrega a un participante.");
 PhaseSession = "INIT";
	if (usuarios == null){
		usuarios = "";	
	}
	
 /** event.getWavelet().getParticipants().add("barbyq14@googlewave.com"); **/
 }
 
 @Override
	public void onGadgetStateChanged(GadgetStateChangedEvent event) {
		Wavelet wave = event.getWavelet();
		Blip blip = event.getBlip();
		Gadget gadget = (Gadget) blip.getElements().get(event.getIndex()); //para saber cual gadget
		usuarios = gadget.getProperty("agregarContacto");
		wave.getParticipants().add(usuarios);
		contador ++;
		String blip1 = wave.reply("\n�Bienvenido!  "+ usuarios).getBlipId();
		
		//para comparar el estado pasado con el presente
		String usuarios2 = gadget.getProperty("agregarContacto");
		if (usuarios2 == null ){
			usuarios2 = "";	
		}
		
		if (contador != numParticipantes){
			PhaseSession = "WAITING";
		wave.getBlip(blip1).append("\nEstamos por comenzar la sesi�n, vamos a esperar a los dem�s. " + PhaseSession);
		}
		
		if (contador == numParticipantes){
			PhaseSession = "READY";
		wave.getBlip(blip1).append("\nTodos los participantes est�n listos, �vamos a comenzar! " + PhaseSession);
		}
		
	}
}